﻿namespace Devonline.Core;

/// <summary>
/// 默认的基础配置
/// </summary>
public class AppSetting
{
    public AppSetting()
    {
        CachePrefix = "dev_";
        CacheExpireTime = AppSettings.UNIT_HOURS_A_DAY;
        Attachment = new();
    }

    /// <summary>
    /// 启动站点配置
    /// </summary>
    public string? Hosts { get; set; }
    /// <summary>
    /// aspnetcore 启动站点配置
    /// </summary>
    public string? Urls { get; set; }
    /// <summary>
    /// 允许访问的地址
    /// </summary>
    public string? AllowedHosts { get; set; }
    /// <summary>
    /// 跨域配置
    /// </summary>
    public string? CorsOrigins { get; set; }
    /// <summary>
    /// 默认密码
    /// </summary>
    public string? DefaultPassword { get; set; }
    /// <summary>
    /// 使用 identity 数据库来启用用户身份认证功能
    /// </summary>
    public bool EnableIdentity { get; set; }
    /// <summary>
    /// 是否启用缓存, 全局缓存
    /// </summary>
    public bool EnableCache { get; set; }
    /// <summary>
    /// 默认应用数据库连接字符串
    /// </summary>
    public string? ApplicationDbContext { get; set; }
    /// <summary>
    /// 默认身份数据库连接字符串
    /// </summary>
    public string? IdentityDbContext { get; set; }
    /// <summary>
    /// 默认缓存数据库连接字符串
    /// </summary>
    public string? CacheConfiguration { get; set; }
    /// <summary>
    /// 消息中心在 redis 中记录的消息前缀
    /// </summary>
    public string? CachePrefix { get; set; }
    /// <summary>
    /// 缓存过期时间, 单位: 小时, 默认一天
    /// </summary>
    public int CacheExpireTime { get; set; }
    /// <summary>
    /// 系统使用的时间类型, 默认: 本地时间
    /// </summary>
    public DateTimeKind TimeKind { get; set; } = DateTimeKind.Local;
    /// <summary>
    /// 发布协议, 此处仅支持 Http 和 Https, 默认 Http
    /// </summary>
    public ProtocolType ProtocolType { get; set; }
    /// <summary>
    /// 数据库类型
    /// </summary>
    public DatabaseType DatabaseType { get; set; }
    /// <summary>
    /// 数据保护级别
    /// </summary>
    public DataProtectionLevel ProtectionLevel { get; set; }

    /// <summary>
    /// 附件相关配置项
    /// </summary>
    public AttachmentOptions Attachment { get; set; }
    /// <summary>
    /// identity options
    /// </summary>
    public IdentityOptions? Identity { get; set; }
    /// <summary>
    /// https certificate configuration
    /// </summary>
    public HttpsCertificateOptions? Certificate { get; set; }
}

/// <summary>
/// 认证相关配置选项
/// </summary>
public class IdentityOptions
{
    public IdentityOptions()
    {
        Authority = "https://localhost:9527";
        SaveTokens = true;
    }

    /// <summary>
    /// 认证服务地址
    /// </summary>
    public string? Authority { get; set; }
    /// <summary>
    /// 客户端编号
    /// </summary>
    public string? ClientId { get; set; }
    /// <summary>
    /// 客户端密钥
    /// </summary>
    public string? ClientSecret { get; set; }
    /// <summary>
    /// 返回值类型
    /// </summary>
    public string? ResponseType { get; set; }
    /// <summary>
    /// Defines whether access and refresh tokens should be stored in the Microsoft.AspNetCore.Authentication.AuthenticationProperties
    ///     after a successful authorization. This property is set to false by default to
    ///     reduce the size of the final authentication cookie.
    /// </summary>
    public bool SaveTokens { get; set; }
}

/// <summary>
/// https certificate configuration
/// </summary>
public class HttpsCertificateOptions
{
    /// <summary>
    /// 域名
    /// </summary>
    public string? Domain { get; set; }
    /// <summary>
    /// 证书地址
    /// </summary>
    public string? Path { get; set; }
    /// <summary>
    /// 证书密码
    /// </summary>
    public string? Password { get; set; }
}

/// <summary>
/// 附件相关选项
/// </summary>
public class AttachmentOptions
{
    /// <summary>
    /// 构造方法
    /// </summary>
    public AttachmentOptions()
    {
        EnableUpload = true;
        EnableImageThumbnail = true;
        RootPath = AppSettings.DEFAULT_ATTACHMENT_PATH;
        AllowFileExtensions = AppSettings.DEFAULT_ALLOW_FILE_EXTENSIONS;
        TempFileExtension = AppSettings.DEFAULT_TEMPORARY_FILE_EXTENSION;
        MaxUploadFileSize = AppSettings.DEFAULT_MAX_UPLOAD_FILE_SIZE;
        TotalUploadFileSize = AppSettings.DEFAULT_TOTAL_UPLOAD_FILE_SIZE;
        ImageThumbnailPrefix = AppSettings.DEFAULT_IMAGE_THUMBNAIL_PREFIX;
        ImageThumbnailSize = AppSettings.DEFAULT_IMAGE_THUMBNAIL_SIZE;
    }

    public bool EnableUpload { get; set; }
    /// <summary>
    /// 附件根路径
    /// </summary>
    public string RootPath { get; set; }
    /// <summary>
    /// 允许上传的附件类型
    /// </summary>
    public string AllowFileExtensions { get; set; }
    /// <summary>
    /// 临时文件扩展名
    /// </summary>
    public string TempFileExtension { get; set; }
    /// <summary>
    /// 单附件文件大小
    /// </summary>
    public int MaxUploadFileSize { get; set; }
    /// <summary>
    /// 总附件文件大小
    /// </summary>
    public int TotalUploadFileSize { get; set; }
    /// <summary>
    /// 启用图片文件自动创建缩略图
    /// </summary>
    public bool EnableImageThumbnail { get; set; }
    /// <summary>
    /// 图片文件缩略图前缀
    /// </summary>
    public string ImageThumbnailPrefix { get; set; }
    /// <summary>
    /// 图片文件缩略图尺寸
    /// </summary>
    public int ImageThumbnailSize { get; set; }
}